<?php
/**
 * @file $HeadURL: forum.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-4-24 ����3:40:14 $
 * @brief 
 */
namespace haibao\cms\business;

use haibao\frame\data\query\Condition;

class Forum extends \haibao\cms\business\Base{
	
	/**
	 * 添加签到
	 * @param array $data
	 */
	public static function addSign($data){
		if(!$data['userId']){
			throw new BusinessException('没有用户');
		}
		if(!$data['credit']){
			throw new BusinessException('没有积分');
		}
		$result = new \haibao\cms\data\Forum();
		$result->addSign($data);
	}
	
	/**
	 * 判断是否有签到
	 * @param Array $data
	 * @return boolean
	 */
	public static function isSign($data){
		if(!$data['userId'] || !$data['credit']){
			return false;
		}
		$result = new \haibao\cms\data\Forum();
		return $result->isSign($data,0);
	}
	
	/**
	 * 到到一周的签到
	 * @param unknown $userId
	 * @throws BusinessException
	 */
	public static function getWeekSign($userId){
		if(!$userId){
			return array();
		}
		$data = new \haibao\cms\data\Forum();
		return $data->getWeekSign($userId);
	}
	
	/**
	* 获取版块发帖数
	* @param int $fid
	*/
	public static function getForumPostNum($fid){
		if(!$fid){
			throw new BusinessException('板块ID不能为空');
		}
		$data = new \haibao\cms\data\Forum();
		return $data->getForumPostNum($fid);
	}

	public static function getForumUsers($page,$name){
		$data = new \haibao\cms\data\Forum();
		return $data->getForumUsers($page,$name);
	}

	public static function checkForumPostId($postId){
		$data = new \haibao\cms\data\Forum();
		return $data->checkForumPostId($postId);
	}

	public static function addBbsPostActivity($model){
		$data = new \haibao\cms\data\Forum();
		$model->CreateBy = self::getCurrentUser()->Id;
		$data->addBbsPostActvity($model);
		self::saveOperateHistory(self::FUNC_CODE_BBS_ACTIVITY_POST,$model->toArray(false));
	}

	public static function getPostList($tid,$page){
		$data = new \haibao\cms\data\Forum();
		return $data->getPostList($tid,$page);
	}

	public static function addActivityPrizePost($tid,$uid){
		//1表示名额已满,2表示已经存在该中奖用户,3表示操作成功
		$resData = 0;
		$getPrize = self::getPrizeByPostId($tid);
		if($getPrize[0] == count($getPrize[1])){
			$resData = 1;
		}elseif(in_array($uid,$getPrize[1])){
			$resData = 2;
		}else{
			$newWinnerIds = implode(',',$getPrize[1]).','.$uid;
			$data = new \haibao\cms\data\Forum();
			$data->addWinnerId($tid,ltrim($newWinnerIds,','));
			$resData = 3;
		}
		return $resData;
	}

	public static function addExcludeUser($tid,$uid){
		$resData = 0;
		$getPrize = self::getPrizeByPostId($tid);
		$newExcludeIds = implode(',',$getPrize[2]).','.$uid;
		if(!in_array($uid,$getPrize[1])){
			$data = new \haibao\cms\data\Forum();
			$data->addExcludeId($tid,ltrim($newExcludeIds,','));
			$resData = 1;
		}
		return $resData;
	}

	public static function getPrizeByPostId($postId){
		$filter = new \haibao\cms\model\filter\BbsPostActivity();
		$filter->select(array(
			$filter::CONDITION_FIELD_NAME_PRIZE_NUM,
			$filter::CONDITION_FIELD_NAME_WINNER_ID,
			$filter::CONDITION_FIELD_NAME_EXCLUDE_ID
		));
		$filter->where($filter::CONDITION_FIELD_NAME_POST_ID,Condition::CONDITION_EQUAL,$postId);
		$data = new \haibao\cms\data\Forum();
		$getWinners = $data->getPrizeByPostId($filter);
		$winnerId = $excludeId = array();
		$prizeNum = 0;
		if($getWinners){
			$winners = $getWinners->WinnerId;
			$winnerId = explode(',',$winners);
			$excludes = $getWinners->ExcludeId;
			$excludeId = explode(',',$excludes);
			$prizeNum = $getWinners->PrizeNum;
		}
		return array($prizeNum,$winnerId,$excludeId);
	}

	public static function getNickNameByIds($uids){
		$data = new \haibao\cms\data\Forum();
		return $data->getNickNameByIds($uids);
	}

	public static function getPrizeSystemMessage($tid,$uid){
		$prizeMsg = '';
		$postInfo = self::getBbsActivityByPostId($tid);
		if($postInfo->IsPushMessage == \haibao\cms\model\data\BbsPostActivity::PUSH_MESSAGE_YES){
			$prizeMsg = $postInfo->Message;
			if(!$prizeMsg){
				$threadUrl = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/thread-'.$postInfo->PostId.'-1-1.html';
				$prizeMsg = '恭喜您在《<a target="_blank" href="'.$threadUrl.'"'.$postInfo->Title.'</a>》活动中被选中为幸运用户,我们将在近日给您邮寄奖品.';
			}
			$spaceUrl = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_USER).'/space/'.$uid.'/pickupaddress.html';
			$prizeMsg .= '请前往个人中心确认收货信息==><a target="_blank" href="'.$spaceUrl.'">收货地址</a>';
		}
		return $prizeMsg;
	}

	public static function addPrizeMessage($tid,$content){
		$data = new \haibao\cms\data\Forum();
		return $data->addPrizeMessage($tid,$content);
	}

	public static function getBbsActivityByPostId($postId){
		$filter = new \haibao\cms\model\filter\BbsPostActivity();
		$filter->select(array(
			$filter::CONDITION_FIELD_NAME_ID,
			$filter::CONDITION_FIELD_NAME_POST_ID,
			$filter::CONDITION_FIELD_NAME_PRIZE_NUM,
			$filter::CONDITION_FIELD_NAME_TITLE,
			$filter::CONDITION_FIELD_NAME_WINNER_ID,
			$filter::CONDITION_FIELD_NAME_CREATE_BY,
			$filter::CONDITION_FIELD_NAME_END_DATE,
			$filter::CONDITION_FIELD_NAME_CREATE_TIME,
			$filter::CONDITION_FIELD_NAME_IS_PUSH_MESSAGE,
			$filter::CONDITION_FIELD_NAME_MESSAGE,
		));
		$filter->where($filter::CONDITION_FIELD_NAME_POST_ID,Condition::CONDITION_EQUAL,$postId);
		$activityInfo = \haibao\cms\business\activity\Activity::getActivityByFilter($filter);
		return $activityInfo;
	}

}